package com.whnk.web.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.whnk.framework.web.base.BaseController;
import com.whnk.system.service.ISysBatchService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

@Controller
@RequestMapping("/system/batch")
public class SysBatchController extends BaseController  {

    private String prefix = "system/batch";

    @Autowired
    private ISysBatchService batchService;

    @RequiresPermissions("system:batch:view")
    @GetMapping()
    public String menu()  {
        return prefix + "/batch";
    }

    @GetMapping("/getTable")
    @ResponseBody
    public List<String> getAllTableName () {
        return batchService.getAllTableName();
    }

    @GetMapping("/getFiled/{tableName}")
    public String getAllFiledName (@PathVariable("tableName") String  tableName,ModelMap mmap) {
        List<String> filedList =  batchService.getAllFiledName(tableName);
        mmap.put("fileds",filedList);
        return prefix + "/filed/select";
    }


    /**
     * 新增保存菜单
     */
    @PostMapping("/public/upload")
    @ResponseBody
    public List<Map<String,Object>> upload(HttpServletRequest request,@RequestParam(value = "fileInput", required = true) MultipartFile file) throws Exception {
        String filedInput = (String)request.getParameter("filedInput");
        String tableSelect = (String) request.getParameter("tableSelect");
        List<Map<String,Object>> exportList = new ArrayList<Map<String,Object>>();
        if(file != null && !file.isEmpty()){
            List<Map<String,String>> filedList = new ArrayList<Map<String,String>>();
            InputStreamReader reader = new InputStreamReader(file.getInputStream(),"UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String line = "";
            line = br.readLine();
            while (line != null) {
                Map<String,String> filedMap = new LinkedHashMap<String,String>();
                if(line.split(",").length == filedInput.split(",").length){
                    for(int i=0 ; i < line.split(",").length ; i++){
                        filedMap.put( filedInput.split(",")[i],line.split(",")[i]);
                    }
                    filedList.add(filedMap);
                } else {
                    throw new Exception("导入的数据和上传的数据不一致,请检查");
                }
                line = br.readLine(); // 一次读入一行数据
            }
            Map<String,Object> filedMap = new HashMap<String,Object>();
            filedMap.put("filedList",filedList);
            exportList.add(filedMap);
        }
        return exportList;
    }

    @PostMapping("/add")
    @ResponseBody
    public String add (@RequestParam("table")  String table,@RequestParam("filedList") String filedList,  HttpServletRequest request) throws Exception {
        String message = "操作成功!";
        ObjectMapper mapper=new ObjectMapper();
        List<Map> list = mapper.readValue(filedList,List.class);
        try {
            for(Map filedMap : list){
                batchService.add(table,filedMap);
            }
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        } catch (Throwable ex) {
            throw new Exception(ex.getMessage());
        }
        return message;
    }


}
